package com.aduerr.v1shouyin.mvp.activity;

import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.aduerr.v1shouyin.App;
import com.aduerr.v1shouyin.R;
import com.aduerr.v1shouyin.entity.CashEntity;
import com.aduerr.v1shouyin.mvp.base.BaseActivity;
import com.aduerr.v1shouyin.mvp.presenter.CashPresenter;
import com.aduerr.v1shouyin.mvp.view.CashView;
import com.aduerr.v1shouyin.util.LogUtils;
import com.aduerr.v1shouyin.util.NetUtils;
import com.aduerr.v1shouyin.util.SharedPreferencesUtils;
import com.aduerr.v1shouyin.util.ToastUtils;
import com.aduerr.v1shouyin.view.BeiZhuDialog;

public class CashResultActivity extends BaseActivity<CashView, CashPresenter> implements CashView {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_ordermoney)
    TextView tvOrdermoney;
    @BindView(R.id.tv_getcash)
    EditText tvGetcash;
    @BindView(R.id.tv_change)
    TextView tvChange;
    @BindView(R.id.tv_confim)
    TextView tvConfim;
    @BindView(R.id.tv_beizhu_cash)
    TextView beizhudailog;
    @BindView(R.id.et_ordermoney)
    EditText ordermoney;


    private Double intrealMoneyDetail;
    private String beizhu;
    private Intent intent;
    private String zhaoling;
    private BeiZhuDialog beiZhuDialog;

    public void initUI() {
        tvTitle.setText("现金收款");
        intent = new Intent(CashResultActivity.this, ResultActivity.class);
    }


    public void initData() {
        intrealMoneyDetail = getIntent().getDoubleExtra("realymoney", 0.00);
        beizhu = getIntent().getStringExtra("beizhu");
        LogUtils.e("intrealMoneyDetail", "" + intrealMoneyDetail);
        if(intrealMoneyDetail == null) {
            ToastUtils.showToast(CashResultActivity.this, "请输入金额");
        } else {
            tvOrdermoney.setText(intrealMoneyDetail + "");
//            ordermoney.setText(intrealMoneyDetail + "");//顶部的需要收取的钱数
        }

    }


    @NonNull
    @Override
    public CashPresenter createPresenter() {
        return new CashPresenter(App.getApp());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        setContentView(R.layout.activity_cash_result);
        ButterKnife.bind(this);

        initUI();
        initData();
        initEvent();
    }


    @OnClick({R.id.img_back, R.id.tv_confim, R.id.tv_beizhu_cash, R.id.et_ordermoney})
    public void onViewClicked(View view) {
        switch(view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_beizhu_cash://弹出备注
                beiZhuDialog = new BeiZhuDialog(CashResultActivity.this,
                        R.style.loading_dialog, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {//取消按钮
                        beiZhuDialog.dismiss();
                    }
                }, new View.OnClickListener() {//确认按钮
                    @Override
                    public void onClick(View v) {
                        beiZhuDialog.dismiss();
                        beizhu = beiZhuDialog.getEditContent();
                    }
                });
                beiZhuDialog.show();
                if(beizhu != null) {
                    beiZhuDialog.setEditeContent(beizhu);
                } else {
                    beiZhuDialog.setEditeContent("");
                }
                break;
            case R.id.et_ordermoney://输入订单金额
                break;
            case R.id.tv_confim:
                //// TODO: 2017/12/7 0007  请求网络
                String realymoney = tvOrdermoney.getText().toString().trim();
                String getcashmoney = tvGetcash.getText().toString().trim();
                if(TextUtils.isEmpty(realymoney) | TextUtils.isEmpty(getcashmoney)) {
                    ToastUtils.showToast(CashResultActivity.this, "请输入金额后再填写收到的金额后重试!");
                    return;
                }
                double Dordermoney = Double.parseDouble(realymoney);
                double DCash = Double.parseDouble(getcashmoney);
                if(DCash < Dordermoney) {
                    ToastUtils.showToast(CashResultActivity.this, "实收金额小于订单金额");
                    return;
                }
                if(zhaoling.contains("E")) {
                    ToastUtils.showToast(CashResultActivity.this, "请输入正确的金额");
                    return;
                }

                getPresenter().getCashresult(CashResultActivity.this, beizhu, realymoney);
                break;
        }
    }


    private void initEvent() {
        //小数点保留两位
//        ordermoney.addTextChangedListener(new MyWatcher(-1, 2));

//        tvGetcash.addTextChangedListener(new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int
//            after) {
//                String trim = ordermoney.getText().toString().trim();
//                if (TextUtils.isEmpty(trim)) {
//                    LogUtils.e("**", s.toString());
//                    ToastUtils.showToast(CashResultActivity.this, "请输入金额后再填写收到的金额!");
//                    return;
//                }
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int
//            count) {
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                if (s != null) {
//                    if (TextUtils.isEmpty(s)) {
//                        ToastUtils.showToast(CashResultActivity.this, "请输入正确的金额");
//                        return;
//                    }
//                    String ordermoneytext = ordermoney.getText().toString();
//                    if (TextUtils.isEmpty(ordermoneytext)) {
//                        ToastUtils.showToast(CashResultActivity.this, "请输入订单金额");
//                        return;
//                    }
//
//                    double Dordermoney = Double.valueOf(ordermoneytext);
//                    double getmoney = Double.parseDouble(s.toString());
//                    LogUtils.e("8822", getmoney + "--------------" + Dordermoney);
//                    if (Dordermoney > getmoney) {
//                        ToastUtils.showToast(CashResultActivity.this, "实收金额不能小于订单金额");
//                        tvGetcash.setText("");
//
//                        tvConfim.setVisibility(View.INVISIBLE);
//                        return;
//                    } else {
//                        zhaoling = (getmoney - Dordermoney) + "";
//                        tvChange.setText(zhaoling);
//                        tvConfim.setVisibility(View.VISIBLE);
//                    }
//
//
//                } else {
//                    ToastUtils.showToast(CashResultActivity.this, "请输入金额");
//                }
//
//
//            }
//        });
        tvGetcash.addTextChangedListener(new TextWatcher() {
                                             private int beforeDot = -1;
                                             private int afterDot = 2;

                                             @Override
                                             public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                                             }

                                             @Override
                                             public void onTextChanged(CharSequence s,
                                                                       int start,
                                                                       int before,
                                                                       int count) {

                                             }

                                             @Override
                                             public void afterTextChanged(Editable s) {
                                                 judge(s);
                                             }

                                             private void judge(Editable editable) {
                                                 String temp = editable.toString();
                                                 int posDot = temp.indexOf(".");
                                                 //直接输入小数点的情况
                                                 if(posDot == 0) {
                                                     editable.insert(0, "0");
                                                     return;
                                                 }
                                                 //连续输入0
                                                 if(temp.equals("00")) {
                                                     editable.delete(1, 2);
                                                     return;
                                                 }
                                                 //输入"08" 等类似情况
                                                 if(temp.startsWith("0") && temp.length() > 1 && (posDot == -1 || posDot > 1)) {
                                                     editable.delete(0, 1);
                                                     return;
                                                 }

                                                 // 不包含小数点 不限制小数点前位数
                                                 if(posDot < 0 && beforeDot == -1) {
                                                     //do nothing 仅仅为了理解逻辑而已
                                                     String ordermoneytext =
                                                             tvOrdermoney.getText().toString();
                                                     String getcash =
                                                             tvGetcash.getText().toString();
                                                     if(TextUtils.isEmpty(ordermoneytext) | TextUtils.isEmpty(getcash)) {
                                                         ToastUtils.showToast(CashResultActivity.this, "请输入正确的金额");
                                                         return;
                                                     }
                                                     double Dordermoney =
                                                             Double.parseDouble(ordermoneytext);
                                                     double DCash =
                                                             Double.parseDouble(getcash);

                                                     zhaoling =
                                                             (DCash - Dordermoney) + "";
                                                     tvChange.setText(zhaoling);
                                                     return;
                                                 } else if(posDot < 0 && beforeDot != -1) {
                                                     //不包含小数点 限制小数点前位数
//                                                     if (temp.length() <= beforeDot) {
//                                                         //do nothing 仅仅为了理解逻辑而已
//                                                     } else {
//                                                         editable.delete(beforeDot,
//                                                         beforeDot + 1);
//                                                     }
//                                                     return;
                                                 }

                                                 //如果包含小数点 限制小数点后位数
                                                 if(temp.length() - posDot - 1 > afterDot && afterDot != -1) {
                                                     editable.delete(posDot + afterDot + 1, posDot + afterDot + 2);//删除小数点后多余位数
                                                 }

                                                 String ordermoneytext =
                                                         tvOrdermoney.getText().toString();
                                                 String getcash =
                                                         tvGetcash.getText().toString();
                                                 if(TextUtils.isEmpty(ordermoneytext) | TextUtils.isEmpty(getcash)) {
                                                     ToastUtils.showToast(CashResultActivity.this, "请输入正确的金额");
                                                     return;
                                                 }
                                                 double Dordermoney =
                                                         Double.parseDouble(ordermoneytext);
                                                 double DCash =
                                                         Double.parseDouble(getcash);

                                                 zhaoling = (DCash - Dordermoney) + "";
                                                 tvChange.setText(zhaoling);
                                             }
                                         }


        );
    }


    @Override
    public void showProgress() {

    }

    @Override
    public void onCompleted() {

    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public void onCashView(CashEntity cashEntity) {

        int success = cashEntity.getSuccess();
        if(success == 1) {
            //// TODO: 2017/12/7 0007  跳转成功的界面
            cashEntity.getOrderid();
            if(intent != null) {
                Log.e("intrealMoneyDetail", intrealMoneyDetail + "");
                intent.putExtra("money", tvOrdermoney.getText().toString().trim());//订单
                intent.putExtra("sucess", "1");
                intent.putExtra("orderid", cashEntity.getOrderid());
                intent.putExtra("zhaoling", zhaoling);//找零
                intent.putExtra("shishou", tvGetcash.getText() + "");//实收
                intent.putExtra("beizhu", beizhu);//
                startActivity(intent);
                finish();
            }
        } else if(success == -100 | success == -200) {
            //// TODO: 2017/12/9 0009 绑定guid  界面
            SharedPreferencesUtils.remove(CashResultActivity.this, "autologin");
            SharedPreferencesUtils.remove(CashResultActivity.this, "remberpassword");
            startActivity(new Intent(CashResultActivity.this, BangGuidActivity.class));
            finish();
        } else if(success == -300 || success == -2 || success == -3 || success == -6) {
            //// TODO: 2017/12/9 0009 去登录的界面
            com.blankj.utilcode.util.ToastUtils.showShortSafe(cashEntity.getErrMsg());
            SharedPreferencesUtils.remove(CashResultActivity.this, "autologin");
            SharedPreferencesUtils.remove(CashResultActivity.this, "remberpassword");
            startActivity(new Intent(CashResultActivity.this, LoginActivity.class));
            finish();
        } else {
            //TODO   跳转到失败的界面
            intent.putExtra("money", intrealMoneyDetail + "");
            intent.putExtra("sucess", "0");
            startActivity(intent);
            finish();

        }

    }

    @Override
    protected void onNetworkDisConnected() {
        ToastUtils.showToast(context, "网络开小差了,请检查网络");
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {
        ToastUtils.showToast(context, type.name());
    }

}
